# !/usr/bin/env python
# encoding: utf-8


"""
  @author: gaogao
  @file: link_calibration_thread.py
  @time: 2022/4/25 15:44
  @desc:
"""
from PyQt5.QtCore import *
import time
from gui import constant
from ATS.switchdriver.LightSwitchDriver import LightSwitchDriver
from ATS.application.LASER_App.N7744C_App import N7744C_App
from ATS.application.LASER_App.L8164B_App import L8164B_App
from ATS.application.LASER_App.N7786C_App import N7786C_App
from utils.save_setting_2_ini import save_ini_info
from ATS.util.calcu_step_util import get_step_list
from utils.read_ini_2_dict import get_option_value, get_refrence_data
from utils.log_util import loggings


class Signal(QObject):
    result_state = pyqtSignal(object)


class LinkCalibrationThread(QRunnable):
    def __init__(self, setting_info):
        super().__init__()
        self.signal = Signal()
        self.setting_info = setting_info
        self.n7744c_app = N7744C_App("SS2")
        self.l8164b_app = L8164B_App("SS3")
        self.n7786c_app = N7786C_App("SS1")
        # self.light_inst = LightSwitchDriver.instance()

    def run(self) -> None:
        try:
            x_data = get_step_list(float(self.setting_info.start_wave), float(self.setting_info.stop_wave),
                                   float(self.setting_info.step_wave))
            points = len(x_data)
            global result
            # self.light_inst.set_all_switch("01", "05")
            # time.sleep(1)
            self.n7744c_app.set_logging_state("STOP")
            # l8164b_app.cls()
            if self.l8164b_app.get_sweep_status() == "+1":
                self.l8164b_app.stop_sweep()
            self.l8164b_app.set_power_unit()
            self.l8164b_app.set_power_value(self.setting_info.power_value)
            time.sleep(4)
            power_input = self.l8164b_app.fetch_input_power()
            self.l8164b_app.set_sweep_mode()
            self.l8164b_app.query_sweep_pmax(float(self.setting_info.start_wave), float(self.setting_info.stop_wave))
            self.l8164b_app.set_repeat_mode()
            self.l8164b_app.set_wave_sweep_speed()
            self.l8164b_app.set_start_stop_step_wave(float(self.setting_info.start_wave),
                                                     float(self.setting_info.stop_wave),
                                                     float(self.setting_info.step_wave))
            self.l8164b_app.trigger_config()
            self.l8164b_app.trigger_output()
            self.l8164b_app.trigger_input()
            self.l8164b_app.output_on()
            self.l8164b_app.set_wave_sweep_state()

            # n7744c_app.reset()
            self.n7744c_app.set_power_unit()
            self.n7744c_app.set_power_range(self.setting_info.power_value)
            self.n7744c_app.set_auto_gain()
            self.n7744c_app.trigger_input_mode()
            self.n7744c_app.set_power_wave_length()
            self.n7744c_app.set_logging_points(points)
            self.n7744c_app.set_logging_state()
            time.sleep(2)
            # self.n7744c_app.tigger_status()
            self.l8164b_app.trigger()
            while True:
                state = self.n7744c_app.get_func_state()
                if state == "LOGGING_STABILITY,COMPLETE":
                    result = self.n7744c_app.get_func_res()
                    break
            self.n7786c_app.stop_logging_polcontroller()
            # self.n7786c_app.set_stabilization_mode(0)
            if self.l8164b_app.get_sweep_status() == "+1":
                self.l8164b_app.stop_sweep()

            result = [float(power_input) - float(i) for i in result]
            if self.setting_info.loss_gc_type == 1:
                section = "loss_sys"
                save_ini_info(constant.REFRENCE_FILE, section, "wave_data", str(x_data))
                save_ini_info(constant.REFRENCE_FILE, section, "power_data", str(result))
            else:
                section = "pd_loss_sys"
                save_ini_info(constant.REFRENCE_FILE, section, "wave_data", str(x_data))
                save_ini_info(constant.REFRENCE_FILE, section, "loss_sys", str(result))

        except Exception as e:
            loggings.error("LinkCalibrationThread_error" + str(e))
            self.signal.result_state.emit(0)
        else:
            self.signal.result_state.emit(1)
